package com.rany.day08.demo07;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class CompareTest {
    public static void main(String[] args) {
        Student lin = new Student("alinwei",26);
        Student lu = new Student("lu",25);
        Student gu = new Student("bgu",26);
        Student huang = new Student("chuang",26);
        ArrayList<Student> list = new ArrayList<>();
        Collections.addAll(list,lin,lu,gu,huang);
        Collections.sort(list, new Comparator<Student>() {
            @Override
            //匿名内部类，重写排序比较方法
            public int compare(Student student, Student t1) {
                int result =  student.getAge() - t1.getAge();
                if (result == 0){
                   return student.getName().charAt(0) - t1.getName().charAt(0);
                }
                return result;
            }
        });
        System.out.println(list);
    }
}
